package org.jeecg.modules.achachievesubjprojitem.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.achachievementsubject.entity.AchAchievementSubjectDemo;
import org.jeecg.modules.achachievementsubject.vo.AchAchievementSubjectVo;
import org.jeecg.modules.achachievesubjprojitem.entity.AchAchievementSubjProjItem;
import org.jeecg.modules.achachievesubjprojitem.entity.AchAchievementSubjProjItemDemo;
import org.jeecg.modules.achachievesubjprojitem.service.IAchAchievementSubjProjItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.achachievesubjprojitem.vo.AchAchievementSubjProjItemVo;
import org.jeecg.modules.common.baseentity.BaseApiEntity;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: ach_achievement_subj_proj_item
 * @author: baochangrong
 * @Date:   2022-03-18
 * @Version: V1.0
 */
@Api(tags="ach_achievement_subj_proj_item")
@RestController
@RequestMapping("/achachievesubjprojitem/achAchievementSubjProjItem")
@Slf4j
public class AchAchievementSubjProjItemController extends JeecgController<AchAchievementSubjProjItem, IAchAchievementSubjProjItemService> {
	@Autowired
	private IAchAchievementSubjProjItemService achAchievementSubjProjItemService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	/**
	 * 分页列表查询
	 *
	 * @param achAchievementSubjProjItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-分页列表查询")
	@ApiOperation(value="ach_achievement_subj_proj_item-分页列表查询", notes="ach_achievement_subj_proj_item-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AchAchievementSubjProjItem achAchievementSubjProjItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AchAchievementSubjProjItem> queryWrapper = QueryGenerator.initQueryWrapper(achAchievementSubjProjItem, req.getParameterMap());
		Page<AchAchievementSubjProjItem> page = new Page<AchAchievementSubjProjItem>(pageNo, pageSize);
		IPage<AchAchievementSubjProjItem> pageList = achAchievementSubjProjItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/** 全部查询 附带关联名称 voList */
	@AutoLog(value = "voList-分页列表查询")
	@ApiOperation(value="voList-分页列表查询", notes="voList-分页列表查询")
	@GetMapping(value = "/voList")
	public Result<?> voList(AchAchievementSubjProjItemVo achAchievementSubjProjItemVo,
							@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							HttpServletRequest req) {
		this.querySysInfo(req,achAchievementSubjProjItemVo);
		Page<AchAchievementSubjProjItemVo> page = new Page<AchAchievementSubjProjItemVo>(pageNo, pageSize);
		IPage<AchAchievementSubjProjItemVo> pageList = achAchievementSubjProjItemService.voList(page, achAchievementSubjProjItemVo);
		return Result.OK(pageList);
	}

	
	/**
	 *   添加
	 *
	 * @param achAchievementSubjProjItem
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-添加")
	@ApiOperation(value="ach_achievement_subj_proj_item-添加", notes="ach_achievement_subj_proj_item-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AchAchievementSubjProjItem achAchievementSubjProjItem,HttpServletRequest req) {
		this.querySysInfo(req,achAchievementSubjProjItem);
		achAchievementSubjProjItem.setUpdateTime(null);
		achAchievementSubjProjItem.setUpdateBy("");
		achAchievementSubjProjItemService.save(achAchievementSubjProjItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param achAchievementSubjProjItem
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-编辑")
	@ApiOperation(value="ach_achievement_subj_proj_item-编辑", notes="ach_achievement_subj_proj_item-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AchAchievementSubjProjItem achAchievementSubjProjItem,HttpServletRequest req) {
		this.querySysInfo(req,achAchievementSubjProjItem);
		achAchievementSubjProjItem.setCreateBy("");
		achAchievementSubjProjItem.setCreateTime(null);
		achAchievementSubjProjItemService.updateById(achAchievementSubjProjItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-通过id删除")
	@ApiOperation(value="ach_achievement_subj_proj_item-通过id删除", notes="ach_achievement_subj_proj_item-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id,HttpServletRequest req) {
		achAchievementSubjProjItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-批量删除")
	@ApiOperation(value="ach_achievement_subj_proj_item-批量删除", notes="ach_achievement_subj_proj_item-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids,HttpServletRequest req) {
		this.achAchievementSubjProjItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ach_achievement_subj_proj_item-通过id查询")
	@ApiOperation(value="ach_achievement_subj_proj_item-通过id查询", notes="ach_achievement_subj_proj_item-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id,HttpServletRequest req) {
		AchAchievementSubjProjItem achAchievementSubjProjItem = achAchievementSubjProjItemService.getById(id);
		if(achAchievementSubjProjItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(achAchievementSubjProjItem);
	}


	 /** 查询导入批次号*/
	 @AutoLog(value = "listBatchNumber-查询导入批次号")
	 @ApiOperation(value="listBatchNumber-查询导入批次号", notes="listBatchNumber-查询导入批次号")
	 @GetMapping(value = "/listBatchNumber")
	 public Result<?> voList(AchAchievementSubjProjItemVo achAchievementSubjProjItemVo,
							 HttpServletRequest req) {
		 return achAchievementSubjProjItemService.listBatchNumber(achAchievementSubjProjItemVo);
	 }


	 /**
	  * 下载模板文件excel
	  *
	  * @param request
	  * @param achAchievementSubjProjItemDemo
	  */
	 @RequestMapping(value = "/exportXlsDemo")
	 public ModelAndView exportXlsDemo(HttpServletRequest request, AchAchievementSubjProjItemDemo achAchievementSubjProjItemDemo){
		 this.querySysInfo(request,achAchievementSubjProjItemDemo);
		 //字典字段，字典编码请写入到这里
		 JSONObject DictList = new JSONObject();
		 DictList.put("成绩状态", "achievement_status");
		 String secondTitle = "分数、成绩状态为必填项，请严格按照要求填写。";
		 return achAchievementSubjProjItemService.exportXlsDemo(request, achAchievementSubjProjItemDemo, "科目分项成绩导入", secondTitle, sysBaseAPI, DictList);
	 }


	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcelDemo", method = RequestMethod.POST)
	 public Result<?> importExcelDemo(HttpServletRequest request, HttpServletResponse response) {
		 BaseApiEntity baseApiEntity = this.querySysInfo(request,new AchAchievementSubjProjItemDemo());
		 return achAchievementSubjProjItemService.importExcelDemo(request, response, achAchievementSubjProjItemService, baseApiEntity);
	 }
    /**
    * 导出excel
    *
    * @param request
    * @param achAchievementSubjProjItem
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AchAchievementSubjProjItem achAchievementSubjProjItem) {
        return super.exportXls(request, achAchievementSubjProjItem, AchAchievementSubjProjItem.class, "ach_achievement_subj_proj_item");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AchAchievementSubjProjItem.class);
    }


	 /** 获取分项成绩数据（根据学生及试卷）
	  * getAchAchieveSubjProjItemList
	  * */
	 @AutoLog(value = "getAchAchieveSubjProjItemList-分页Vo列表查询")
	 @ApiOperation(value="getAchAchieveSubjProjItemList-分页Vo列表查询", notes="getAchAchieveSubjProjItemList-分页Vo列表查询")
	 @GetMapping(value = "/getAchAchieveSubjProjItemList")
	 public Result<?> getAchAchieveSubjProjItemList(String studentId, String examinationPaperId,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 String tenantId = req.getHeader("tenant-id");
		 AchAchievementSubjProjItemVo achAchievementSubjProjItemVo = new AchAchievementSubjProjItemVo();
		 Page<AchAchievementSubjProjItemVo> page = new Page<AchAchievementSubjProjItemVo>(pageNo, pageSize);
		 IPage<AchAchievementSubjProjItemVo> pageList = achAchievementSubjProjItemService.getAchAchieveSubjProjItemList(page, studentId,examinationPaperId,tenantId);
		 return Result.OK(pageList);
	 }

	 /**
	  * 从request获取 token 用户信息，组织信息
	  *
	  * @param req
	  * @return
	  */
	 public BaseApiEntity querySysInfo(HttpServletRequest req,Object achAchievementSubjProjItemDemo) {

		 BaseApiEntity baseApiEntity = new BaseApiEntity();

		 //获取当前登录用户信息
		 String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String username = JwtUtil.getUsername(token);
		 baseApiEntity.setUsername(username);
		 baseApiEntity.setCreateBy(username);
		 baseApiEntity.setUpdateBy(username);

		 LoginUser loginUser = sysBaseAPI.getUserByName(username); //.getUserById() 获取登录用户
		 baseApiEntity.setLoginUser(loginUser);
		 String sysOrgCode = loginUser.getOrgCode();//获取当前用户的当前的部门编码（不是id是编码）,该编码随前端切换
		 String tenantId = req.getHeader("tenant-id");//获取当前登录用户的租户id
		 String schoolYearId = req.getHeader("school-year-id");//获取当前登录系统选择的学年
		 String semesterId = req.getHeader("semester-id");//获取当前登录系统选择的学期
		 baseApiEntity.setTenantId(tenantId);
		 baseApiEntity.setSchoolYearId(schoolYearId);
		 baseApiEntity.setSemesterId(semesterId);
		 String departId = sysBaseAPI.getDepartIdsByOrgCode(sysOrgCode);//获取当前用户设置的部门id
		 List<String> subDepIdsList = sysBaseAPI.getSubDepIdsByDepId(departId); //获取下级部门的部门id，自定义改造sysBaseAPI
		 baseApiEntity.setSysOrgCode(sysOrgCode);
		 baseApiEntity.setSubDepIdsList(subDepIdsList);
		 String subDepSysOrgCodes = sysBaseAPI.getSubDepOrgCodesByDepId(departId); //直接获取下级部门的orgcode，自定义改造sysBaseAPI
		 baseApiEntity.setSubDepSysOrgCodes(subDepSysOrgCodes);
		 //userIdentity 用户身份字典码user_identity， 3教师 4学生 1普通用户 2上级部门负责人 5家长 6班级
		 int userIdentity = loginUser.getUserIdentity();//获取当前登录用户身份：字典码 user_identity
		 baseApiEntity.setUserIdentity(userIdentity);
		 //给业务对象赋 创建人、修改人、部门编码的值
		 BeanUtils.copyProperties(baseApiEntity,achAchievementSubjProjItemDemo);

		 return baseApiEntity;
	 }

}
